
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;

public class FishingFlatFileSource extends FishingDataSource {

    public void initialize() {
        loadFish();
        loadBucket();
    }

    public void loadBucket() {
        synchronized (bucketLock) {
            buckets = new ArrayList<Bucket>();
            String location = FishingMod.getInstance().getBucketLocation();

            if (new File(location).exists()) {
                try {
                    Scanner scanner = new Scanner(new File(location));
                    while (scanner.hasNextLine()) {
                        String line = scanner.nextLine();
                        if (line.startsWith("#") || line.equals("")) {
                            continue;
                        }
                        String[] split = line.split(":");

                        Bucket bucket = new Bucket();
                        bucket.name = split[0];
                        bucket.itemNum = Integer.parseInt(split[1]);
                        bucket.itemAmt = Integer.parseInt(split[2]);
                        buckets.add(bucket);
                    }
                    scanner.close();
                } catch (Exception e) {
                    log.log(Level.SEVERE, "Exception while reading " + location, e);
                }
            }
        }
    }

    public void loadFish() {
        synchronized (fishLock) {
            fishes = new ArrayList<Fish>();
            tiers = new ArrayList<Tier>();
            String location = FishingMod.getInstance().getFishLocation();

            if (new File(location).exists()) {
                try {
                    Scanner scanner = new Scanner(new File(location));
                    while (scanner.hasNextLine()) {
                        String line = scanner.nextLine();
                        if (line.startsWith("#") || line.equals("")) {
                            continue;
                        }
                        String[] split = line.split(":");

                        if (split[0].equals("catch")) {
                            fishChance = Integer.parseInt(split[1]);
                        } else if (split[0].equals("tier")) {
                            Tier tier = new Tier();
                            tier.tier = Integer.parseInt(split[1]);
                            tier.minroll = Integer.parseInt(split[2]);
                            tier.maxroll = Integer.parseInt(split[3]);
                            tiers.add(tier);
                        } else {
                            Fish fish = new Fish();
                            fish.itemNumber = Integer.parseInt(split[0]);
                            fish.name = split[1];
                            fish.tier = Integer.parseInt(split[2]);
                            fish.minroll = Integer.parseInt(split[3]);
                            fish.maxroll = Integer.parseInt(split[4]);
                            fish.small = Integer.parseInt(split[5]);
                            fish.big = Integer.parseInt(split[6]);
                            fish.huge = Integer.parseInt(split[7]);
                            fishes.add(fish);
                        }
                    }
                    scanner.close();
                } catch (Exception e) {
                    log.log(Level.SEVERE, "Exception while reading " + location, e);
                }
            }
        }
    }

    public synchronized void addBucket(Bucket bucket) {
        String bucketLoc = FishingMod.getInstance().getBucketLocation();
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(bucketLoc, true));
            StringBuilder builder = new StringBuilder();
            builder.append(bucket.name);
            builder.append(":");
            builder.append(bucket.itemNum);
            builder.append(":");
            builder.append(bucket.itemAmt);
            bw.append(builder.toString());
            bw.newLine();
            bw.close();

            synchronized (bucketLock) {
                buckets.add(bucket);
            }
        } catch (Exception e2) {
            log.log(Level.SEVERE, "Exception while writing new customer to " + bucketLoc, e2);
        }
    }

    public synchronized void removeBucket(Bucket bucket) {
        FileWriter writer = null;
        String bucketLoc = FishingMod.getInstance().getBucketLocation();
        try {
            // Now to save...
            BufferedReader reader = new BufferedReader(new FileReader(new File(bucketLoc)));
            StringBuilder toWrite = new StringBuilder();
            String line = "";
            while ((line = reader.readLine()) != null) {
                if (!line.contains(bucket.name+":"+bucket.itemNum)) {
                    toWrite.append(line).append("\r\n");
                }
            }
            reader.close();

            writer = new FileWriter(bucketLoc);
            writer.write(toWrite.toString());
            writer.close();
        } catch (Exception e1) {
            log.log(Level.SEVERE, "Exception while delete warp from " + bucketLoc, e1);
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException ex) {
            }
        }

        synchronized (bucketLock) {
            buckets.remove(bucket);
        }
    }
}
